import * as unitService from '@/services/unit';
import { message } from 'antd';
import { DEFAULT_RESPONE_SUCCESS } from '@/constants';

export default {
  namespace: 'unit',

  state: {
    current: 1,
    total: 0,
    data: [],
    rowKey: 'id',
    // 职务
    pageNo: 1,
    duty_name: "",
    unitId: ""
  },

  subscriptions: {

  },

  effects: {
    *resetPassword({ payload }, { call, put }) {
      const { body, callback } = payload
      const response = yield call(unitService.resetPassword, { ...body })
      if (response && response.status === DEFAULT_RESPONE_SUCCESS && response.result) {
        callback(true)
      } else {
        callback(false)
      }
    },
    *queryData({ payload }, { call, put }) {
      const { callback, current = 1, pageSize, ...extra } = payload;

      const response = yield call(unitService.queryData, { pageNo: current, pageSize }, { ...extra });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { content: data = [], totalElements: total = 0 } = result;

        if (typeof callback === 'function') {
          callback(data);
        } else {
          yield put({
            type: 'changeState',
            payload: {
              data,
              current,
              pageSize,
              total
            }
          });
        }
      }
    },
    *saveOrUpdateData({ payload }, { call, put }) {
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: Array.isArray(data) ? data : [data]
      };
      let response = {};
      if (!data.id) {
        response = yield call(unitService.saveData, option);
      } else {
        response = yield call(unitService.updateData, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success(!data.id ? '新增单位成功' : '编辑单位成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} }
        });
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *deleteData({ payload }, { call, put }) {
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: Array.isArray(data) ? data : [data]
      };
      const response = yield call(unitService.updateData, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('停用单位成功', 1);
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },

    // 职务管理 - 查询接口
    *queryDataTitle({ payload }, { call, put }) {
      const { callback, ...extra } = payload;

      const response = yield call(unitService.queryDataTitle, { ...extra });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { data = [], count } = result;

        if (typeof callback === 'function') {
          callback(data);
        } else {
          yield put({
            type: 'changeState',
            payload: {
              data,
              pageNo: payload.pageNo,
              pageSize: payload.pageSize,
              count,
              unitId: payload.unit_id
            }
          });
        }
      }
    },

    // 职务管理 - 模糊查询接口
    *queryDataTitleByMohu({ payload }, { call, put }) {
      const { callback, ...extra } = payload;

      const response = yield call(unitService.queryDataTitleByMohu, { ...extra });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { data = [], count } = result;

        if (typeof callback === 'function') {
          callback(data);
        } else {
          yield put({
            type: 'changeState',
            payload: {
              duty_name: payload.duty_name,
              data,
              pageNo: payload.pageNo,
              pageSize: payload.pageSize,
              count,
              unitId: payload.unit_id
            }
          });
        }
      }
    },
    // 职务管理 - 删除
    *deleteDataTitle({ payload }, { call, put, select }) {
      const { callback, ...extra } = payload;
      const mohu = yield select(state => state.unit.duty_name);
      const unitId = yield select(state => state.unit.unitId);
      const pageNo = yield select(state => state.unit.pageNo);
      const response = yield call(unitService.deleteDataTitle, { ...extra });
      if (response && response.result === 1) {
        message.success('删除成功');
        yield put({ type: 'queryDataTitleByMohu', payload: { unit_id: unitId, duty_name: mohu, pageNo: pageNo, pageSize: 10 } });
      } else if (response && response.result == -1) {
        message.error('当前职务正在使用、删除失败！');
      } else {
        message.error('删除失败');
      }
    },
    // 职务管理 - 批量删除
    *updateIsExpiredListTitle({ payload }, { call, put, select }) {
      const { callback, list } = payload;
      const mohu = yield select(state => state.unit.duty_name);
      const unitId = yield select(state => state.unit.unitId);
      const pageNo = yield select(state => state.unit.pageNo);
      const response = yield call(unitService.updateIsExpiredListTitle, list);
      if (response && response.result === 1) {
        message.success('删除成功');
        yield put({ type: 'queryDataTitleByMohu', payload: { unit_id: unitId, duty_name: mohu, pageNo: pageNo, pageSize: 10 } });
      } else if (response && response.result == -1) {
        message.error('当前职务正在使用、删除失败！');
      } else {
        message.error('删除失败');
      }
    },
    // 职务管理 - 添加
    *saveDutyTitle({ payload }, { call, put, select }) {
      const { callback, ...extra } = payload;
      const mohu = yield select(state => state.unit.duty_name);
      const unitId = yield select(state => state.unit.unitId);
      const response = yield call(unitService.saveDutyTitle, { ...extra });
      if (response && response.result === 1) {
        message.success('添加成功');
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} }
        });
        yield put({ type: 'queryDataTitleByMohu', payload: { unit_id: unitId, duty_name: mohu, pageNo: 1, pageSize: 10 } });
      } else if (response && response.result == -1) {
        message.error('该职务已存在');
      } else {
        message.error('删除失败');
      }
    },

    // 职务管理 - 修改
    *updateDutyTitle({ payload }, { call, put, select }) {
      const { callback, ...extra } = payload;
      const mohu = yield select(state => state.unit.duty_name);
      const unitId = yield select(state => state.unit.unitId);
      const response = yield call(unitService.updateDutyTitle, { ...extra });
      if (response && response.result === 1) {
        message.success('修改成功');
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} }
        });
        yield put({ type: 'queryDataTitleByMohu', payload: { unit_id: unitId, duty_name: mohu, pageNo: 1, pageSize: 10 } });
      } else if (response && response.result == -1) {
        message.error('该职务已存在');
      } else {
        message.error('修改失败');
      }
    },
  },

  reducers: {
    clear(state, { payload }) {
      return {
        current: 1,
        total: 0,
        data: [],
        rowKey: 'id'
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  },
};
